package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleMapper;
import com.example.forum.dao.ArticleReplyMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.ArticleReply;
import com.example.forum.services.IArticleReplyService;

import com.example.forum.services.IArticleService;
import com.example.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.RescaleOp;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Autowired
    private ArticleReplyMapper articleReplyMapper;
    @Autowired
    private IArticleService iArticleService;


    @Override
    public void create(ArticleReply articleReply) {
        if(articleReply==null||articleReply.getArticleId()==null
        ||articleReply.getPostUserId()==null
        || StringUtil.isEmpty(articleReply.getContent())){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(0);
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        Date date = new Date();
        articleReply.setUpdateTime(date);
        articleReply.setCreateTime(date);
        int i = articleReplyMapper.insertSelective(articleReply);
        if(i!=1){
            log.info("回复失败");
            throw new ApplicationException(AppResult.failed("回复帖子失败"));
        }
        iArticleService.addOnceReplayArticleCountById(articleReply.getArticleId());


    }

    //获取帖子回复列表详情
    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        if(articleId==null||articleId<=0){
            log.warn("传入的articleId不正确");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<ArticleReply> result= articleReplyMapper.selectByArticleId(articleId);
        return result;

    }
}
